package com.whfc.ms.api.fim.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.fim.dto.FimAlgCheckTypeDTO;
import com.whfc.fim.dto.FimWarnRuleDTO;
import com.whfc.fim.entity.FimWarnRule;
import com.whfc.fim.param.FimSetUserParam;
import com.whfc.fim.service.FimWarnRuleService;
import com.whfc.fuum.dto.SysAlgorithmTypeDTO;
import com.whfc.fuum.service.SysAlgorithmService;
import com.whfc.ms.api.fim.service.MsFimWarnRuleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报警设置 service
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-09-08 20:06
 */
@Service
public class MsFimWarnRuleServiceImpl implements MsFimWarnRuleService {

    @DubboReference(interfaceClass = FimWarnRuleService.class, version = "1.0.0")
    private FimWarnRuleService fimWarnRuleService;

    @DubboReference(interfaceClass = SysAlgorithmService.class, version = "1.0.0")
    private SysAlgorithmService sysAlgorithmService;


    @Override
    public PageData<FimWarnRuleDTO> list(Integer deptId, Integer pageNum, Integer pageSize) {
        return fimWarnRuleService.list(deptId, pageNum, pageSize);
    }

    @Override
    public ListData<FimWarnRuleDTO> list(Integer deptId) {
        return fimWarnRuleService.list(deptId);
    }

    @Override
    public void setUser(FimSetUserParam request) {
        fimWarnRuleService.setUser(request);
    }

    /**
     * 获取组织机构已配置的算法类型列表
     *
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @Return
     */
    @Override
    public PageData<SysAlgorithmTypeDTO> getAlgorithmConfList(Integer deptId, Integer pageNum, Integer pageSize) throws BizException {

        List<FimAlgCheckTypeDTO> fimAlgTypeDTOS = fimWarnRuleService.listAlgCheckType(deptId);
        if (fimAlgTypeDTOS != null && fimAlgTypeDTOS.size() > 0) {
            List<Integer> typeList = new ArrayList<>();
            for (FimAlgCheckTypeDTO fimAlgTypeDTO : fimAlgTypeDTOS) {
                typeList.add(fimAlgTypeDTO.getAlgCheckType());
            }
            //组织机构已配置的算法类型列表
            PageHelper.startPage(pageNum, pageSize);
            List<SysAlgorithmTypeDTO> list = sysAlgorithmService.getAlgorithmInTypes(typeList);
            PageHelper.clearPage();
            return PageUtil.pageData(PageInfo.of(list));
        }
        return PageUtil.pageData(PageInfo.of(new ArrayList<>()));
    }

    /**
     * 获取组织机构未配置的算法类型列表
     *
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @Return
     */
    @Override
    public PageData<SysAlgorithmTypeDTO> getAlgorithmUnConfList(Integer deptId, Integer pageNum, Integer pageSize) throws BizException {
        List<FimAlgCheckTypeDTO> fimAlgTypeDTOS = fimWarnRuleService.listAlgCheckType(deptId);
        if (fimAlgTypeDTOS != null && fimAlgTypeDTOS.size() > 0) {
            List<Integer> types = new ArrayList<>();
            for (FimAlgCheckTypeDTO fimAlgTypeDTO : fimAlgTypeDTOS) {
                types.add(fimAlgTypeDTO.getAlgCheckType());
            }
            //组织机构已配置的算法类型列表
            PageHelper.startPage(pageNum, pageSize);
            List<SysAlgorithmTypeDTO> list = sysAlgorithmService.getAlgorithmNotInTypes(types);
            PageHelper.clearPage();
            return PageUtil.pageData(PageInfo.of(list));
        }
        return PageUtil.pageData(PageInfo.of(new ArrayList<>()));
    }

    /**
     * 添加组织机构配置算法类型
     *
     * @param deptId
     * @param types  算法类型集合
     */
    @Override
    public void addAlgorithmConf(Integer deptId, List<Integer> types) {
        if (types != null && types.size() > 0) {
            //根据算法代码集合查询
            List<SysAlgorithmTypeDTO> sysAlgorithmTypes = sysAlgorithmService.getAlgorithmInTypes(types);
            if (sysAlgorithmTypes != null && sysAlgorithmTypes.size() > 0) {
                List<FimWarnRule> list = new ArrayList<>();
                for (SysAlgorithmTypeDTO algorithmType : sysAlgorithmTypes) {
                    FimWarnRule fimWarnRule = new FimWarnRule();
                    fimWarnRule.setDeptId(deptId);
                    fimWarnRule.setName(algorithmType.getName());
                    fimWarnRule.setType(algorithmType.getType());
                    fimWarnRule.setDelFlag(DelFlag.UNDELETE.getValue());
                    Date date = new Date();
                    fimWarnRule.setCreateTime(date);
                    fimWarnRule.setUpdateTime(date);
                    list.add(fimWarnRule);
                }
                fimWarnRuleService.batchInsertAlgorithmType(list);
            }
        }
    }

    /**
     * 组织机构删除配置算法类型
     *
     * @param deptId
     * @param type
     */
    @Override
    public void delectAlgorithmConf(Integer deptId, Integer type) {
        fimWarnRuleService.removeAlgorithmByType(deptId, type);
    }

}
